package com.ease.plugin.pluginClasses {
	import com.ease.core.plugin.ResourceBase;
	import com.ease.plugin.PluginFactory;
	import com.ease.plugin.elements.SWFLoader;
	import com.ease.plugin.events.ErrorEvent;
	import com.ease.plugin.events.LoaderEvent;
	import com.ease.plugin.loaderClasses.LoaderLoadTrait;
	import com.ease.plugin.traits.LoadState;
	import com.ease.plugin.traits.LoadTrait;

	import flash.display.DisplayObject;
	import flash.events.Event;

	import mx.core.Application;
	import mx.core.FlexGlobals;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;

	internal class DynamicPluginLoader extends PluginLoader {
		public function DynamicPluginLoader(mediaFactory:PluginFactory) {
			super(mediaFactory);
		}

		/**
		 * @private
		 */
	    override public function canHandleResource(resource:ResourceBase):Boolean {
	    	return new SWFLoader().canHandleResource(resource);
	    }

		/**
		 * @private
		 */
		override protected function executeLoad(loadTrait:LoadTrait):void {
			updateLoadTrait(loadTrait, LoadState.LOADING);

			var swfLoader:SWFLoader = new SWFLoader();
			swfLoader.addEventListener(LoaderEvent.LOAD_STATE_CHANGE, onSWFLoaderStateChange);

			var loaderLoadTrait:LoaderLoadTrait = new LoaderLoadTrait(swfLoader, loadTrait.resource);
			loaderLoadTrait.addEventListener(ErrorEvent.MEDIA_ERROR, onLoadError);

			swfLoader.load(loaderLoadTrait);

			function onSWFLoaderStateChange(event:LoaderEvent):void {
				if (event.newState == LoadState.READY) {
					swfLoader.removeEventListener(LoaderEvent.LOAD_STATE_CHANGE, onSWFLoaderStateChange);
					loaderLoadTrait.removeEventListener(ErrorEvent.MEDIA_ERROR, onLoadError);

					loadPluginInfo();

				}
				else if (event.newState == LoadState.LOAD_ERROR) {
					swfLoader.removeEventListener(LoaderEvent.LOAD_STATE_CHANGE, onSWFLoaderStateChange);

					updateLoadTrait(loadTrait, event.newState);
				}
			}

			function onLoadError(event:ErrorEvent):void {
				loaderLoadTrait.removeEventListener(ErrorEvent.MEDIA_ERROR, onLoadError);

				loadTrait.dispatchEvent(event.clone());
			}

			function loadPluginInfo():void{
			 	var UI:UIComponent = new UIComponent();
				UI.visible = false;
				UI.includeInLayout = false;
				UI.addChild(loaderLoadTrait.loader.content);
            FlexGlobals.topLevelApplication.addChild(UI);
            loaderLoadTrait.loader.content.addEventListener(FlexEvent.APPLICATION_COMPLETE, onApplicationReady);
			}

			function onApplicationReady(event:Event):void {
				 var app:Application = event.target.application;
             loaderLoadTrait.loader.content.removeEventListener(FlexEvent.APPLICATION_COMPLETE, onApplicationReady);
			    loadFromPluginInfo(loadTrait, app, loaderLoadTrait.loader);
			}
		}

		override protected function executeUnload(loadTrait:LoadTrait):void {
			updateLoadTrait(loadTrait, LoadState.UNLOADING);
			var pluginLoadTrait:PluginLoadTrait = loadTrait as PluginLoadTrait;
			unloadFromPluginInfo(pluginLoadTrait.pluginInfo);
			updateLoadTrait(loadTrait, LoadState.UNINITIALIZED);
		}

		private function validateLoadedContent(displayObject:DisplayObject):Boolean {
			return true;
		}

	}
}